package com.example.jbmsuppliers.sinopec.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.UpdateVersion;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DoubleButtonDialog;
import com.jbm.jbmsupplier.util.IAppVersion;

import butterknife.ButterKnife;
import butterknife.InjectView;


/**
 * 李征
 * Created by 家帮忙 on 2017/1/9.
 */

public class SplashActivity extends OvertActivity {

    private static final int GO_HOME = 0001;
    private static final int GO_GUIDE = 0002;
    public static final int VISITION = 0003;
    private static final int NO_INTERINT = 0004;
    private final int NoNETWORK = 0005;
    public final static int PHOTO_LOGINS = 0001;
    @InjectView(R.id.splashiv)
    ImageView splashiv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
//            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            Window window = getWindow();
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
//                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//            window.getDecorView().setSystemUiVisibility(
//                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                            | View.SYSTEM_UI_FLAG_FULLSCREEN
//                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.setStatusBarColor(Color.red(R.color.white));

            setContentView(R.layout.activity_splash);
            ButterKnife.inject(this);
            ExitAppUtlis.getInstance().addActivity(this);



                 updatevision();





            Animation animationUtils = AnimationUtils.loadAnimation(this, R.anim.splash_anim);
            splashiv.startAnimation(animationUtils);
//        }
    }

    /**
     * 检查版本更新
     */
    private void updatevision() {

        PackageManager packagename = getPackageManager();
        try {
            PackageInfo packageinfo = packagename.getPackageInfo(getPackageName(), 0);
            String vision = packageinfo.versionName;
            String star = vision.substring(0, vision.indexOf("."));
            String end = vision.substring(vision.indexOf(".") + 1, vision.length());
//            mDialog.show();
            int connInt = setConnectivityManager();
            if (connInt == 0) {//无网络
                prologue();
            } else if (connInt == 1) {//有网络
                NetWork network = new NetWork(handerl);
                network.getVision(Integer.parseInt(star), Integer.parseInt(end));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 判断第几次进入APP,分别跳到那个页面
     */
    private void prologue() {
        SharedPreferences sharedPreferences = getSharedPreferences("splash", Activity.MODE_PRIVATE);
        boolean isFirstIn = sharedPreferences.getBoolean("isFirstIn", true);
        if (isFirstIn) {
            handerl.sendMessage(handerl.obtainMessage(GO_GUIDE));
        } else if (!isFirstIn) {
            handerl.sendMessage(handerl.obtainMessage(GO_HOME));
        }


    }

    /**
     * 跳到主页面
     */
    private void goHome() {
        startActivity(new Intent(SplashActivity.this, MainActivity.class));
        finish();
    }

    /**
     * 跳到引导页
     */
    private void goGuide() {
        startActivity(new Intent(SplashActivity.this, GuideActivity.class));
        finish();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            if(grantResults[0]==0){
                new UpdateVersion(SplashActivity.this, url);
                prologue();
            }else{
               finish();
            }


    }

    private String url;
    private Handler handerl = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case GO_HOME: {
                    goHome();
                    break;
                }
                case GO_GUIDE: {
                    goGuide();
                    break;
                }
                case VISITION: {
                    final IAppVersion result = (IAppVersion) msg.obj;
                    if (result != null) {
                        if (result.back && !result.app_url.isEmpty()) {
                         url = result.app_url;
                            final DoubleButtonDialog dialog = new DoubleButtonDialog(SplashActivity.this, "您有新版本是否要更新...");
                            dialog.setNoText("下次更新");
                            dialog.setTetle_message("更新新版本");
                            dialog.setisVisibilitymess();
                            dialog.getokbtn().setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    if (Build.VERSION.SDK_INT >= 23) {
                                        int i = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                                        if(i==0){
                                            new UpdateVersion(SplashActivity.this, url);
                                            prologue();
                                           
                                        }else{
                                            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
                                        }
                                    }else{
                                        new UpdateVersion(SplashActivity.this, url);
                                        prologue();
                                    }
                                    dialog.dismiss();

                                }
                            });
                            dialog.getnobtn().setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (result.milestone > 0) {
                                        finish();
                                    } else {
                                        prologue();

                                    }
                                    dialog.dismiss();
                                }
                            });
                        } else if (result.equals("网络超时")){
                            prologue();
                        }else{
                            prologue();
                        }
                    }


                    break;
                }
                case NO_INTERINT: {
                    //   NoInterInt();
                    break;
                }
                case NoNETWORK: {
                    prologue();
                    break;
                }
            }
        }
    };

}
